let express = require('express');
let router = express.Router();
let URL = require('url');
let mysql = require('mysql');
let bodyParser = require('body-parser');
let cookieParser = require('cookie-parser');
let session = require('cookie-session');
router.use(bodyParser.urlencoded({extended: true}));
// var User = require('./user'); // get
let mysqlhelp = require("../common/sql");

// 保存文章
router.post('/saveArticle', function (req, res, next) {
    let article = req.body;
    let title = article.title;
    let img = article.img;
    let date = article.date;
    let details = article.details;
    let aritice_introduction = article.aritice_introduction;
    let id = article.id;
    let sql = ''; // 执行的sql
    let parameter = [article.title, article.img, article.date, article.details, article.aritice_introduction, article.id]; // 请求的参数
    // parameter =  [article.title, article.img, article.date, article.details, article.aritice_introduction, article.id];
    if (id == 0) { // 新增
        sql = `insert into aritice_index (title, img, date, details, aritice_introduction ) VALUES (?, ?, ?, ?, ?)`;
    } else {
        sql = `update aritice_index set title = ? , img = ? , date= ? , details= ? , aritice_introduction = ? where id= ?`;
    }
    // select的promise方法执行完毕执行then方法
    mysqlhelp.select(sql, parameter)
        .then(function (data) {
            // // 用户名密码匹配
            // if(data.result.length !=0 && data.result[0].password == password){
            //   data.isError = false
            // }else{
            //   data.isError = true
            // }
            data.isEffective = true;
            res.send(data)
        }).catch(function (err) {
        err.isEffective = true;
        res.send(err)
    })
});
module.exports = router;
